package com.luoying.itew.adapter;

import android.content.Context;
import android.net.Uri;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.luoying.itew.R;

import java.util.List;

/**
 * Created by lufan on 2019-06-17
 * Class describe
 */
public class PhotoAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    /**
     * 最大选择数量
     */
    public static final int MAX_SELECT = 3;

    private final int ITEM_TYPE_IMG = 1;
    private final int ITEM_TYPE_ADD = 2;

    public List<String> pathList;
    public List<Uri> uriList;
    private Context context;
    public AdapterListener listener;


    public PhotoAdapter(Context context, List<String> pathList, List<Uri> uriList) {
        this.pathList = pathList;
        this.uriList = uriList;
        this.context = context;
    }

    public void setListener(AdapterListener listener) {
        this.listener = listener;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view;
        if(viewType==ITEM_TYPE_IMG){
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_phone_log,parent,false);
            return  new ViewHolder(view);
        } else {
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_photo_add,parent,false);
            return  new AddViewHolder(view);
        }
    }

    @Override
    public int getItemViewType(int position) {
        if(pathList.size()==0){
            return ITEM_TYPE_ADD;
        } else {
            return position==pathList.size()? ITEM_TYPE_ADD:ITEM_TYPE_IMG;
        }
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        if(getItemViewType(position)==ITEM_TYPE_IMG){
           ViewHolder imgVh = (ViewHolder) holder;
            Glide.with(context)
                    .load(uriList.get(position))
                    .into(imgVh.imageView);
            if(listener!=null){
                imgVh.delImageView.setOnClickListener(v ->listener.onDeleteClick(position));
                imgVh.imageView.setOnClickListener(v -> listener.onImageClick(position));
            }

        } else {
            AddViewHolder addViewHolder = (AddViewHolder) holder;
            if(listener!=null){
                addViewHolder.rootView.setOnClickListener(v -> {
                    listener.onAddImage();
                });
            }
        }
    }



    @Override
    public int getItemCount() {
        if(pathList.size()<MAX_SELECT){
            return pathList.size()+1;
        } else {
            return pathList.size();
        }
    }

    public class ViewHolder extends RecyclerView.ViewHolder{
        ImageView imageView;
        ImageView delImageView;

        public ViewHolder(View itemView) {
            super(itemView);
            imageView = itemView.findViewById(R.id.photo_img);
            delImageView = itemView.findViewById(R.id.photo_delete);
        }
    }

    public class AddViewHolder extends RecyclerView.ViewHolder{
        View rootView;
        public AddViewHolder(View itemView) {
            super(itemView);
            rootView = itemView;
        }
    }

    public interface AdapterListener{
        void onAddImage();
        void onDeleteClick(int index);
        void onImageClick(int index);
    }
}
